package com.vogtec.fota.httpclient;

import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.api.Response;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

public interface HttpClientService {

	/**
	 * 构造OPTIONS请求
	 * @param uri
	 * @return
	 * @throws IllegalArgumentException URI不合法
	 */
	public Request OPTIONS(String uri) throws IllegalArgumentException;

	/**
	 * 构造GET请求
	 * @param uri
	 * @return
	 * @throws IllegalArgumentException URI不合法
	 */
	public Request GET(String uri) throws IllegalArgumentException;
	
	/**
	 * 构造PUT请求
	 * @param uri
	 * @param contentType Content-Type头域的值，如果传入null会使用默认值text/plain
	 * @param body 消息体
	 * @return
	 * @throws IllegalArgumentException URI不合法
	 */
	public Request PUT(String uri, String contentType, String body) throws IllegalArgumentException;
	
	/**
	 * 构造DELETE请求
	 * @param uri
	 * @return
	 * @throws IllegalArgumentException
	 */
	public Request DELETE(String uri) throws IllegalArgumentException;
	
	/**
	 * 构造POST请求
	 * @param uri
	 * @param contentType Content-Type头域的值，如果传入null会使用默认值text/plain
	 * @param body 消息体
	 * @return
	 * @throws IllegalArgumentException URI不合法
	 */
	public Request POST(String uri, String contentType, String body) throws IllegalArgumentException;
	
	/**
	 * 构造发送文件的POST请求
	 * @param uri
	 * @param file
	 * @return
	 * @throws IllegalArgumentException URI不合法
	 * @throws IOException
	 */
	public Request POSTFile(String uri, File file) throws IllegalArgumentException, IOException;

	/**
	 * 为待发送的请求添加事件监听器
	 * @param request
	 * @param asynListener 事件监听器
	 * @return
	 */
	public Request listener(Request request, Request.Listener.Adapter asynListener);
	
	/**
	 * <b>Blocking API，在响应返回或者超时之前调用该方法的线程会一直阻塞</b>
	 * <p>
	 * 发送HTTP请求，等待响应超时时间timeout
	 * @param request
	 * @param timeout 超时时间（秒）
	 * @return HTTP响应
	 * @throws TimeoutException 发送超时
	 * @throws IOException
	 */
	public ContentResponse send(Request request, int timeout) throws TimeoutException, IOException;
	
	/**
	 * <b>Non Blocking API，发送HTTP请求后立即返回，异步等待响应时间，该方法不会阻塞调用线程</b>
	 * @param request
	 * @param asynListener 异步响应时间监听器
	 */
	public void send(Request request, Response.Listener.Adapter asynListener);
}
